// pages/batchQuery/batchQuery.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        personList: [],
        queryTime: "",
        hitIndex: false,
        notes: [{
                content: "批量查询是根据13位的申请编码精准查询,不是根据备注查询"
            },
            {
                content: "个人摇号编码有效期是3个月,请及时登录官网申请延长有效期"
            },
            {
                content: "批量查询只支持个人增量指标查询"
            },
            {
                content: "摇号结果只查询最近三个月"
            }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      
    },

    onShow: function(options) {
        var queryTime = wx.getStorageSync("queryTime");
        if(queryTime){
            this.setData({
                queryTime:queryTime
            })
        }

        //获取摇号人员列表
        this.getLotteryPersonList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        wx.hideLoading();
    },
    getLotteryPersonList: function() {
        wx.showLoading({
            title: '数据加载中',
        });
        //设置默认值
        var that = this;
        var token = wx.getStorageSync("token");
        console.log("token:", token);
        var url = util.baseUrl + "/car/getLotteryPersonList";
        var data = {
            "openId": token
        }
        console.log("url:", url);
        console.log("data:", data);

        wx.request({
            url: url,
            method: 'GET',
            data: data,
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    that.setData({
                        personList: res.data
                    });
                    console.log("personList:", that.data.personList);
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
                that.popupMsg("服务器有点忙,请稍后再试");
            }
        })
    },

    batchQueryIndex: function() {
        wx.showLoading({
            title: '数据查询中',
        });

        //设置默认值
        var that = this;
        var token = wx.getStorageSync("token");
        var url = util.baseUrl + "/car/batchQueryIndex";
        var data = {
            "openId": token
        }
        console.log("url:", url);
        console.log("data:", data);
        wx.request({
            url: url,
            data: data,
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false,
                        confirmText: '确定'
                    });
                } else {
                    var queryTime = util.formatTime(new Date());
                    that.setData({
                        personList: res.data,
                        hitIndex: false,
                        queryTime: queryTime
                    });
                    wx.setStorageSync("queryTime", queryTime);
                    var personList = that.data.personList;
                    for (var i = 0; i < personList.length; i++) {
                        var person = personList[i];
                        if (person.status == 1) {
                            that.setData({
                                hitIndex: true
                            });
                            break;
                        }
                    }

                    if (that.data.hitIndex) {
                        wx.showModal({
                            title: '恭喜您',
                            content: '有人中签啦',
                            showCancel: false
                        });
                    } else {
                        wx.showModal({
                            title: '很遗憾',
                            content: '没有人中签',
                            showCancel: false
                        })
                    }

                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
                that.popupMsg("服务器有点忙,请稍后再试");
            },
            complete: function(e) {
                wx.hideLoading();
            }
        })
    },

    goSaveLotteryPerson: function() {
        wx.navigateTo({
            url: '../lotteryPersonInfo/lotteryPersonInfo',
        })
    },
})